package com.jmemoir.safe.paramsencrypt.utils;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * RSA 加解密工具类
 *
 * @author Tellsea
 * @date 2023/8/9
 */
public class RsaUtils {

    public static KeyPair generateKeyPair() throws Exception {
        // 生成RSA密钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    }

    public static String encrypt(String message, String publicKey) throws Exception {
        return encrypt(message, convertStringToPublicKey(publicKey));
    }

    public static String encrypt(String message, PublicKey publicKey) throws Exception {
        // 使用公钥加密消息
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(message.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedMessage, String privateKey) throws Exception {
        return decrypt(encryptedMessage, convertStringToPrivateKey(privateKey));
    }

    public static String decrypt(String encryptedMessage, PrivateKey privateKey) throws Exception {
        // 使用私钥解密消息
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedMessage.replaceAll("\r|\n", ""));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return new String(cipher.doFinal(encryptedBytes));
    }

    public static PublicKey convertStringToPublicKey(String publicKeyString) throws Exception {
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }

    public static PrivateKey convertStringToPrivateKey(String privateKeyString) throws Exception {
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyString);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }

    public static void main(String[] args) throws Exception {
        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
        System.out.println("Public Key: " + publicKeyString);
        String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
        System.out.println("Private Key: " + privateKeyString);
        // Public Key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkXY5nh0NDo+Cc03kJxQzAWT8LPFSmOsDcolqDXLKW/tONg5EVxtMCj8sPUtnycg6jx7VymWaFMaX2hVQJKLkNfGY5ryezWtDTnCSUCLHhXRRAB8+5QFeL9PVpRHQPtpnpYPHpvzbG+1Bt9t/NN9pjegwyRUfy/s49iSmQqJMlx5kaRi/oykShyKLSq28E+eJfhwZW2wOr/CO4yJKMfmpWcHx6wqk+74ni2bgbMUAWo6GG07vDFddAo79YsI+KZ7LCBFszvNIjm6JRYyiLX73tCNRbEkC5xWBOIo/aubVxgu9b/G1i5kBPaCFRFp3egayM6QrdTIBd9qbUs6d+ObR3QIDAQAB
        // Private Key: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCRdjmeHQ0Oj4JzTeQnFDMBZPws8VKY6wNyiWoNcspb+042DkRXG0wKPyw9S2fJyDqPHtXKZZoUxpfaFVAkouQ18ZjmvJ7Na0NOcJJQIseFdFEAHz7lAV4v09WlEdA+2melg8em/Nsb7UG3238032mN6DDJFR/L+zj2JKZCokyXHmRpGL+jKRKHIotKrbwT54l+HBlbbA6v8I7jIkox+alZwfHrCqT7vieLZuBsxQBajoYbTu8MV10Cjv1iwj4pnssIEWzO80iObolFjKItfve0I1FsSQLnFYE4ij9q5tXGC71v8bWLmQE9oIVEWnd6BrIzpCt1MgF32ptSzp345tHdAgMBAAECggEAVwTo9yPd+cX0flIsmQ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